package com.gkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkd.constant.MessageConstant;
import com.gkd.constant.StatusContant;
import com.gkd.context.BaseContext;
import com.gkd.exception.AlreadyExistsException;
import com.gkd.pojo.dto.FavoriteDTO;
import com.gkd.pojo.dto.FavoritePageQueryDTO;
import com.gkd.pojo.entity.Favorite;
import com.gkd.pojo.entity.Likes;
import com.gkd.pojo.vo.FavoriteVO;
import com.gkd.result.PageResult;
import com.gkd.service.FavoriteService;
import com.gkd.mapper.FavoriteMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author ZJF
* @description 针对表【favorite】的数据库操作Service实现
* @createDate 2024-06-15 18:18:29
*/
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite>
    implements FavoriteService{

    @Autowired
    private FavoriteMapper favoriteMapper;

    /**
     * 添加收藏
     * @param postId
     */
    @Override
    public void add(Integer postId) {
        Favorite favorite = Favorite.builder()
                .postId(postId)
                .userId(BaseContext.getCurrentId())
                .createTime(new Date())
                .build();

        LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorite::getPostId,favorite.getPostId());
        wrapper.eq(Favorite::getUserId,favorite.getUserId());
        Favorite one = favoriteMapper.selectOne(wrapper);
        if (one!=null){
            throw new AlreadyExistsException(MessageConstant.FAVORITE_BE_RELATED);
        }

        favoriteMapper.insert(favorite);
    }

    /**
     * 分页查询
     * @param favoritePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(FavoritePageQueryDTO favoritePageQueryDTO) {
        Page<FavoriteVO> page = new Page<>(favoritePageQueryDTO.getCurrent(), favoritePageQueryDTO.getPageSize());
        IPage<FavoriteVO> favoriteIPage = favoriteMapper.selectFavoritePage(page,favoritePageQueryDTO);
        return new PageResult(favoriteIPage.getTotal(),favoriteIPage.getRecords());
    }

    /**
     * 通过文章id和用户id获取收藏
     * @param postId
     * @param userId
     * @return
     */
    @Override
    public String getByPostIdAndUserId(Integer postId, Integer userId) {
        LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorite::getPostId,postId);
        wrapper.eq(Favorite::getUserId,userId);

        Favorite favorite = favoriteMapper.selectOne(wrapper);
        if (favorite!=null){
            return StatusContant.FAVORITE_TRUE;
        }else {
            return StatusContant.FAVORITE_FALSE;
        }
    }

    /**
     * 取消收藏
     * @param postId
     */
    @Override
    public void delete(Integer postId) {
        LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorite::getPostId,postId);
        wrapper.eq(Favorite::getUserId,BaseContext.getCurrentId());

        favoriteMapper.delete(wrapper);
    }
}




